class CommunityController < ApplicationController
  helper :profile

  def index
    @title = "Community"
    @letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")
    if params[:id]
      @initial = params[:id]
      specs = Spec.find(:all,
                        :conditions => ["last_name like ?", @initial+'%'],
                        :order => "last_name, first_name")
      users = specs.collect { |spec| spec.user }
      @users = users.paginate :per_page => 10, :page => params[:page]
    end
  end

  def browse
  end

  def search
    @title = "Search RailsSpace"
    if params[:q]
      query = params[:q]
      begin
        # First find the user hits...
        @users = User.find_by_contents(query, :limit => :all)
        # ...then the subhits.
        specs = Spec.find_by_contents(query, :limit => :all)
        faqs = Faq.find_by_contents(query, :limit => :all)

        # Now combine into one list of distinct users sorted by last name.
        hits = specs + faqs
        @users.concat(hits.collect { |hit| hit.user }).uniq!
        # Sort by last name (requires a spec for each user).
        @users.each { |user| user.spec ||= Spec.new }
        @users = @users.sort_by { |user| user.spec.last_name }
        @users = @users.paginate :per_page => 10, :page => params[:page]    
      rescue Ferret::QueryParser::QueryParseException => e
        @invalid = true
      end
      
    end
  end

end
